package com.zanfeng.signleplatform.dao.persistence.dao;

import com.zanfeng.signleplatform.api.result.modules.module.ModuleResult;
import com.zanfeng.signleplatform.api.result.modules.rolePermission.RolePermissionResult;
import com.zanfeng.signleplatform.base.BaseMapper;
import com.zanfeng.signleplatform.dao.persistence.entity.RolePermission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface RolePermissionMapper extends BaseMapper<RolePermission> {

    //  列表查询
    List<RolePermissionResult> queryRolePermissionList(Map paramMap);

    //  根据ID列表逻辑删除
    void deleteRolePermissionListByIdList(List<Integer> idList);

    //  查询role_id对应的权限id列表
    @Select("select permission_id from wt_role_permission where role_id = #{roleId}")
    List<Integer> queryPermissionIdListByRoleId(@Param("roleId") Integer roleId);

    //  删除role_id相关数据
    @Select("delete from wt_role_permission where role_id = #{roleId}")
    void deleteByRoleId(@Param("roleId") Integer roleId);

    //  查询role对应的权限code列表
    @Select("SELECT wp.`permission_code` FROM wt_role_permission wrp, wt_permission wp " +
            "WHERE wrp.`permission_id` = wp.id AND wrp.role_id = #{roleId} AND wp.`is_publish` = 1 AND wp.`is_delete` = 0 ORDER BY wp.`module_id`, wp.`order_num` DESC")
    List<String> queryPermissionCodeListByRoleId(@Param("roleId") Integer roleId);

    //  查询role对应的可见模块列表
    @Select("SELECT wm.* FROM wt_permission wp, wt_role_permission wrp, wt_module wm " +
            "WHERE wp.id = wrp.`permission_id` AND wp.`module_id` = wm.id " +
            "AND wm.is_publish = 1 and wm.is_delete = 0 AND wrp.`role_id` = #{roleId}  GROUP BY wp.`module_id` ORDER BY wm.module_level , wm.order_num DESC")
    List<ModuleResult> queryModuleResultListByRoleId(@Param("roleId") Integer roleId);

    //  查询二级permission列表
    List<Integer> queryParentPermissionIdList(List<Integer> idList);
}